package com.xuecheng.search.controller;

import com.xuecheng.api.search.EsCourseControllerApi;
import com.xuecheng.framework.domain.course.CoursePub;
import com.xuecheng.framework.domain.course.response.TeachplanMediaPub;
import com.xuecheng.framework.domain.search.CourseSearchParam;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.search.service.EsCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/search/course")
public class EsCourseController implements EsCourseControllerApi {

    @Autowired
    EsCourseService esCourseService;

    /**
     * 综合搜索
     * @param page 页码数
     * @param size 每页记录数
     * @param courseSearchParam 搜索条件
     * @return 搜索结果
     * @throws IOException
     */
    @Override
    @GetMapping("/list/{page}/{size}")
    public QueryResponseResult list(@PathVariable("page") int page,
                                    @PathVariable("size") int size,
                                    CourseSearchParam courseSearchParam) throws IOException {
        return esCourseService.list(page, size, courseSearchParam);
    }

    /**
     * 根据课程id查询课程计划, 通过es查询
     * 视频播放页面展示课程计划列表, 点击可以播放视频
     * @param courseId
     * @return
     */
    @Override
    @GetMapping("/getall/{courseId}")
    public Map<String, CoursePub> getAll(@PathVariable("courseId") String courseId) {
        return esCourseService.getAll(courseId);
    }

    /**
     * 根据课程计划id查询对应的媒资信息
     * @param teachplanId 课程计划id
     * @return 课程计划对应的媒资信息
     */
    @Override
    @GetMapping("/getmedia/{teachplanId}")
    public TeachplanMediaPub getMediaByTeachplanId(@PathVariable("teachplanId") String teachplanId) {
        String[] teachplanIds = new String[]{teachplanId};
        QueryResponseResult result = esCourseService.getMediaByTeachplanIds(teachplanIds);
        if (result == null) {
            return null;
        }
        QueryResult queryResult = result.getQueryResult();
        if (queryResult == null) {
            return null;
        }
        List list = queryResult.getList();
        if (list.size() <= 0) {
            return null;
        }
        return (TeachplanMediaPub)(list.get(0));
    }
}
